from conan import ConanFile
from conan.tools.apple import fix_apple_shared_install_name
from conan.tools.build import cross_building
from conan.tools.env import VirtualBuildEnv
from conan.tools.gnu import PkgConfigDeps
from conan.tools.files import (
    apply_conandata_patches,
    copy,
    export_conandata_patches,
    get,
    rename,
    rm,
    rmdir
)
from conan.tools.gnu import Autotools, AutotoolsToolchain
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc, unix_path
from conan.tools.scm import Version
import os

required_conan_version = ">=1.54.0"


class LibstropheConan(ConanFile):
    name = "libstrophe"
    description = "A simple, lightweight C library for writing XMPP clients"
    license = ("MIT", "GPL-3.0")
    url = "https://github.com/conan-io/conan-center-index"
    homepage = "http://strophe.im/libstrophe"
    topics = ("XMPP", "clients")

    package_type = "library"
    settings = "os", "arch", "compiler", "build_type"
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
    }
    default_options = {
        "shared": False,
        "fPIC": True,
    }

    @property
    def _settings_build(self):
        return getattr(self, "settings_build", self.settings)

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")

    def build_requirements(self):
        self.tool_requires("libtool/2.4.7")
        if not self.conf.get("tools.gnu:pkg_config", check_type=str):
            self.tool_requires("pkgconf/2.0.2")
        if self.settings.os in [ "tvOS", "watchOS" ]:
            self.tool_requires("gnu-config/cci.20210814")
        if self._settings_build.os == "Windows":
            self.win_bash = True
            if not self.conf.get("tools.microsoft.bash:path", check_type=str):
                self.tool_requires("msys2/cci.latest")
     

    def requirements(self):
        #self.requires("gnutls/3.7.8")
        self.requires("openssl/3.1.4")
        self.requires("expat/[>=2.0.0]")
        self.requires("libxml2/[>=2.7]")
            
    def source(self):
        get(self, **self.conan_data["sources"][self.version], strip_root=True)

    def generate(self):
        env = VirtualBuildEnv(self)
        env.generate()

        tc = AutotoolsToolchain(self)
        #path = unix_path(self, self.dependencies["openssl"].package_folder)
        tc.configure_args.append("--disable-examples")
        env = tc.environment()
        env.prepend_path("PKG_CONFIG_PATH", self.generators_folder)
        tc.generate(env)
        pc = PkgConfigDeps(self)
        pc.generate()

    def build(self):
        apply_conandata_patches(self)
        autotools = Autotools(self)
        autotools.autoreconf()
        autotools.configure()
        autotools.make()

    def package(self):
        copy(self, "LICENSE.txt", self.source_folder, os.path.join(self.package_folder, "licenses"))
        copy(self, "GPL-LICENSE.txt", self.source_folder, os.path.join(self.package_folder, "licenses"))
        copy(self, "MIT-LICENSE.txt", self.source_folder, os.path.join(self.package_folder, "licenses"))
        autotools = Autotools(self)
        autotools.install()

    def package_info(self):    
        self.cpp_info.set_property("cmake_find_mode", "both")
        self.cpp_info.set_property("cmake_file_name", "libstrophe")
        self.cpp_info.set_property("cmake_target_name", "strophe")
        self.cpp_info.set_property("pkg_config_name", "libstrophe")
        self.cpp_info.libs = ["strophe"]

        # TODO: to remove in conan v2
        self.cpp_info.names["cmake_find_package"] = "libstrophe"
        self.cpp_info.names["cmake_find_package_multi"] = "libstrophe"
